﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace EP.Base.TaskTools.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            
            //var tkpool = new BaseTaskHandle(() => Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")), 5, () => Console.WriteLine("结束")) { IsWait = true };
            //tkpool.Start();

            TestNormalLoopHandle();
            Console.WriteLine("结束1");
            Console.ReadKey();
        }

        private static void TestNormal()
        {
            var list = new List<int>();
            for (int i = 0; i < 20; i++)
            {
                list.Add(i);
            }
            Action<int> action =
                data => Console.WriteLine(data + "__" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));

            var tkPool = new NormalTaskHandle<int>(list, action, 5, () => Console.WriteLine("结束")) { IsWait = false };

            tkPool.Start();
            //tkPool.Stop();
        }
        private static void TestNormalLoopHandle()
        {
            var list = new List<int>();
            for (int i = 0; i < 3; i++)
            {
                list.Add(i);
            }
            Action<int> action =data =>
            {
                while (true)
                {
                    Console.WriteLine(data + "__" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")+"__"+Thread.CurrentThread.ManagedThreadId); 
                    Thread.Sleep(2000);
                }
            };

            var tkPool = new NormalTaskHandle<int>(list, action, 5, () => Console.WriteLine("结束")) { IsWait = false };

            tkPool.Start();
            //tkPool.Stop();
        }
        private static void TestTrigger()
        {
            
            Action<IEnumerable<long>> action = (datas) =>
            {
                string num = Guid.NewGuid().ToString();

                Console.WriteLine("一个批次" + num);
                foreach (var data in datas)
                {
                    Console.WriteLine(data);
                }
                Console.WriteLine("一个批次" + num);
            };

            long index = 0;
            var task = new TriggerTaskHandle<long>(action, 5)
            {
                CumulativeCount=10,
                IsCumulativeTrigger = true,
                IsTimeTrigger = true,
                TimeInterval = 3000,
                BatchMaxCount = 10
                
            };
            task.Start();
            
                Task.Run(() =>
                {
                    while (true)
                    {
                        index++;
                        Console.WriteLine("写入" + index);
                        task.InsertData(index);
                        Thread.Sleep(100);
                    }

                });
        }


        private static void TestLoop()
        {
            Action<long> action =
                Console.WriteLine;
            Func<IEnumerable<long>, IEnumerable<long>> getdata = (lastdata) =>
            {
                var data = new List<long>();
                long lastid = lastdata != null ? lastdata.Max() : 0;
                for (int i = 0; i < 5; i++)
                {
                    data.Add(lastid + i);
                }
                return data;
            };
            var tkPool = new LoopTaskHandle<long>(getdata, action, 10, () => Console.WriteLine("结束")) { GetDataInterval = 0 };
            tkPool.Start();
            tkPool.Stop();
        }
    }
}
